package cn.crm.web.controller.business;

import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.system.domain.pojo.dto.ProdectDto;
import cn.crm.system.domain.pojo.dto.SeriesDto;
import cn.crm.system.domain.pojo.dto.SpecificationDto;
import cn.crm.system.domain.pojo.vo.ProdectVo;
import cn.crm.system.domain.pojo.vo.SeriesVo;
import cn.crm.system.service.IProdectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/prodect")
@Api(tags = "产品模块/产品列表")
public class ProectController  extends BaseController {

    @Autowired
    private IProdectService prodectService;


    @PreAuthorize("@ss.hasPermi('system:prodect:add')")
    @PostMapping("/add")
    @ApiOperation("添加产品")
    @Log(title = "产品分类管理", businessType = BusinessType.INSERT)
    public AjaxResult addSeries(@RequestBody ProdectDto prodectDto) {
        log.debug("开始处理添加产品资料的接口");
        int i = prodectService.insertProdect(prodectDto);
        if (i < 1){
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");
    }
    @GetMapping("/list")
    @ApiOperation("查询prodect资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:list')")
    @Log(title = "产品分类管理", businessType = BusinessType.OTHER)
    public TableDataInfo Getserieslist(ProdectDto prodectDto) {
        log.debug("开始处理查询产品资料的接口");
        startPage();
        List<ProdectVo> prodectVos = prodectService.selectProdect(prodectDto);
        return getDataTable(prodectVos);
    }

    @Log(title = "产品分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{specificationId}")
    @ApiOperation("删除产品资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:delete')")
    public AjaxResult delete(@PathVariable Long[] id,
                             @PathVariable Long[] specificationId ){
        log.debug("开始删除表单资料的接口");
        //删除表头数据

        int deletespecification = prodectService.deletespecification(id, specificationId);

        if (deletespecification < 1){
            return AjaxResult.error("删除产品资料失败");
        }
        return AjaxResult.success("删除产品资料成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询产品资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:query')")
    @Log(title = "产品分类管理", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable Long id){
        log.debug("开始通过id查询产品资料的接口");
        ProdectVo prodectVo = prodectService.selectProdectById(id);
        return  success(prodectVo);
    }

    @Log(title = "产品分类管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改产品资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:update')")
    public AjaxResult update( @RequestBody ProdectDto prodectDto){
        log.debug("开始修改产品参数资料的接口");
        int i = prodectService.updateProdect(prodectDto);
        if (i < 1){
            return AjaxResult.error("修改产品参数失败");
        }
        return AjaxResult.success("修改产品资料成功");
    }

}
